using System;
using System.Collections.Generic;
using System.Linq;
using GameFramework.Battle.Core;
using UnityEditor;
using UnityEditor.AnimatedValues;
using UnityEngine;
using Yoozoo.Gameplay;
using Yoozoo.Gameplay.RTS;
using Yoozoo.Gameplay.RTS.Proto;
using Yoozoo.Gta.Common;

namespace Gameplay.RTS.Editor
{
    public class WonderEditorWindow : EditorWindow
    {
        private string x;
        private string y;
        private string x2;
        private string y2;
        private MarchEntity virtualMarch;

        private GUILayoutOption[] nonAutoAlignOption;

        private GUILayoutOption[] inputOption;
        private void OnEnable()
        {
            x = PlayerPrefs.GetString("WonderEditorX");
            y = PlayerPrefs.GetString("WonderEditorY");
            x2 = PlayerPrefs.GetString("WonderEditorX2");
            y2 = PlayerPrefs.GetString("WonderEditorY2");
        }
        
        private void OnGUI()
        {
            if (nonAutoAlignOption == null)
            {
                nonAutoAlignOption = new GUILayoutOption[]
                {
                    GUILayout.ExpandWidth(false),
                };
            }

            if (inputOption == null)
            {
                inputOption = new GUILayoutOption[]
                {
                    GUILayout.ExpandWidth(false),
                    GUILayout.Width(50),
                };
            }
            GUILayout.BeginHorizontal();
            GUILayout.Label("a侧X:",nonAutoAlignOption);
            x = GUILayout.TextField(x,inputOption);
            GUILayout.Space(20);
            GUILayout.Label("a侧Y:",nonAutoAlignOption);
            y = GUILayout.TextField(y,inputOption);
            GUILayout.EndHorizontal();
            
            GUILayout.BeginHorizontal();
            GUILayout.Label("b侧X:",nonAutoAlignOption);
            x2 = GUILayout.TextField(x2,inputOption);
            GUILayout.Space(20);
            GUILayout.Label("b侧Y:",nonAutoAlignOption);
            y2 = GUILayout.TextField(y2,inputOption);
            GUILayout.EndHorizontal();
            
            if (GUILayout.Button("创建关卡虚拟部队"))
            {
                float.TryParse(x, out var xAxis);
                float.TryParse(y, out var yAxis);
                float.TryParse(x2, out var x2Axis);
                float.TryParse(y2, out var y2Axis);
                MarchManager.Instance.cachedTestAction = () => { StartCreateVirtualMarch(xAxis, yAxis); };
            }

            if (GUILayout.Button("创建警备部队"))
            {
                MarchManager.Instance.cachedTestAction = () => { CreateWonderPoliceUnit(); };
            }

            if (GUILayout.Button("警备部队移动"))
            {
                MarchManager.Instance.cachedTestAction = () => { PoliceMove(); };
            }
            
            if (GUILayout.Button("创建增援部队"))
            {
                MarchManager.Instance.cachedTestAction = () => { CreateWonderAssistUnit(); };
            }
            
            if (GUILayout.Button("移动增援部队"))
            {
                MarchManager.Instance.cachedTestAction = () => { AssistMove(); };
            }
            
            if (GUILayout.Button("过桥"))
            {
                MarchManager.Instance.cachedTestAction = () => { TestBridge(); };
            }
            
            /*if (GUILayout.Button("创建突击部队"))
            {
                MarchManager.Instance.cachedTestAction = () => { CreateWonderStrikeUnit(); };
            }*/
        }

        long troopId = 99996666;
        private void StartCreateVirtualMarch(float x,float y)
        {
            if (virtualMarch != null)
            {
                return;
            }

           
            ResidentHandler.Inst.GetFunction("marchTest.CreateVirtualBridgeUnit").Action(troopId,x,y);
            /*var serverInfo = new TroopMarch();
            serverInfo.lod2 = new TroopMarchL2
            {
                marchTypeInt = (int)MarchType.BRIDGE_13,
                soldierCount = 10000,
                initSoldierCount = 10000,
                soldierType = 1,
            };
            serverInfo.lod4 = new TroopMarchL4
            {
                tid = troopId,
                hero1 = 41001,
                coordinate = new Coordinate
                {
                    xAxis = x,
                    zAxis = y,
                },
                marchStatusInt = (int)MarchStatus.SIEGE_FIGHT_6,
                team = 2,
                visible = true,
                clientStatusInt = (int)EMarchStatus.SIEGE_FIGHT,
            };
            var soldiers = new LuaArrAccess();
            MarchManager.Instance.CreateMarch(troopId, 2, EClientMarchType.Bridge);
            MarchManager.Instance.UpdateTroopMarchInfo(serverInfo,soldiers);
            virtualMarch = MarchManager.Instance.GetMarchEntityById(troopId);*/
            
            PlayerPrefs.SetString("WonderEditorX",x.ToString());
            PlayerPrefs.SetString("WonderEditorY",y.ToString());
            PlayerPrefs.SetString("WonderEditorX2",x2.ToString());
            PlayerPrefs.SetString("WonderEditorY2",y2.ToString());
        }

        private long guardId = 90001;
        private Vector3 guardPosition;
        private void CreateWonderPoliceUnit()
        {
            guardPosition = BattleUtils.GetGroundPositionByScreenPosition(WorldCameraManager.mainCamera,
                new Vector3(Screen.width / 2f, Screen.height / 2f));
            ResidentHandler.Inst.GetFunction("marchTest.CreateWonderGuardUnit").Action(guardId,guardPosition.x,guardPosition.z);
        }

        private void PoliceMove()
        {
            var oldPosition = guardPosition;
            var offset = UnityEngine.Random.insideUnitCircle * 5;
            var newPosition = guardPosition + new Vector3(offset.x, 0, offset.y);
            ResidentHandler.Inst.GetFunction("marchTest.MoveWonderGuardUnit").Action(guardId,oldPosition.x,oldPosition.z,newPosition.x,newPosition.z);
            guardPosition = newPosition;
        }

        private long assistId = 90002;
        private Vector3 assistPosition;
        private void CreateWonderAssistUnit()
        {
            assistPosition = BattleUtils.GetGroundPositionByScreenPosition(WorldCameraManager.mainCamera,
                new Vector3(Screen.width / 2f, Screen.height / 2f));
            ResidentHandler.Inst.GetFunction("marchTest.CreateWonderAssistUnit").Action(assistId,assistPosition.x,assistPosition.z);
        }
        
        private void AssistMove()
        {
            var oldPosition = assistPosition;
            var offset = UnityEngine.Random.insideUnitCircle * 5;
            var newPosition = assistPosition + new Vector3(offset.x, 0, offset.y);
            ResidentHandler.Inst.GetFunction("marchTest.MoveWonderAssistUnit").Action(assistId,oldPosition.x,oldPosition.z,newPosition.x,newPosition.z);
            assistPosition = newPosition;
        }

        private void TestBridge()
        {
            /*var marches = MarchManager.Instance.Marches;
            for (int i = 0; i < marches.Count; i++)
            {
                var march = marches[i];
                if (march.Team == 0)
                {
                    var data = march.RtsServerInfo;
                    var screenPoint1 = new Vector3(Screen.width * 0.3f, Screen.height * 0.3f);
                    var position1 = BattleUtils.GetGroundPositionByScreenPosition(WorldCameraManager.mainCamera,screenPoint1);
                    var screenPoint2 = new Vector3(Screen.width * 0.7f, Screen.height * 0.7f);
                    var position2 = BattleUtils.GetGroundPositionByScreenPosition(WorldCameraManager.mainCamera,screenPoint2);
                    data.crossDetail = new CrossingGate
                    {
                        start = new Coordinate
                        {
                            xAxis = position1.x,
                            zAxis = position1.z,
                        },
                        end = new Coordinate
                        {
                            xAxis = position2.x,
                            zAxis = position2.z,
                        },
                        startTime = TimeUtils.GetServerTimeMs(),
                    };
                    MarchManager.Instance.UpdateTroopMarchInfo(data,new LuaArrAccess());
                    break;
                }
            }*/
        }
    }
}